package com.bootlever.mq.producer.controller;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.bootlever.framework.rocketmq.templates.RocketmqTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RocketmqController {

    private static final Logger logger = LoggerFactory.getLogger(RocketmqController.class);
    private int i = 0;

    @Autowired
    private RocketmqTemplate rocketmqTemplate;

    @RequestMapping(value = "/send")
    public String send() throws Exception {
        DefaultMQProducer producer = (DefaultMQProducer) rocketmqTemplate.getDefaultMQProducer("test_producer");
        try {
            // topic,tag ,body
            i ++;
            Message msg = new Message("topic1",
                        "TagA",("Hello RocketMQ" + i).getBytes());
            SendResult sendResult = producer.send(msg);
            logger.info("sendResult:{}",sendResult);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return "发送成功";
    }
}
